package org.cable.account.listener;

import com.rabbitmq.client.Channel;
import io.netty.handler.codec.compression.Bzip2Decoder;
import lombok.extern.slf4j.Slf4j;
import org.cable.account.service.TrafficService;
import org.cable.common.enums.BizCodeEnum;
import org.cable.common.exception.BizException;
import org.cable.common.model.EventMessage;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author hsj
 * @date 2023/12/1 9:25
 */
@Component
@RabbitListener(queuesToDeclare = {
	@Queue("order.traffic.queue"),
	@Queue("traffic.free_init.queue")
})
@Slf4j
public class TrafficMqListener {



	@Autowired
	private TrafficService trafficService;

	@RabbitHandler
   public void trafficHandler(EventMessage eventMessage, Message message, Channel channel){

	   log.info("监听到trafficHandler:{}",eventMessage);

	   try {

		   trafficService.handlerTrafficMessage(eventMessage);
	   }catch (Exception e){
		   log.error("消费者失败:{}",e);
		   throw new BizException(BizCodeEnum.MQ_CONSUMER_EXCEPTION);
	   }

	   log.info("消费成功:{}",eventMessage);
   }

}
